package com.example.service;

import com.example.bean.Merchant;
import com.example.bean.MerchantVo;
import com.example.bean.Result;
import com.example.mapper.MerchantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class MerchantService {
    @Autowired
    MerchantMapper merchantMapper;

    public Result login(String username, String password) {
        Merchant mer = merchantMapper.SelectMerchant(username, password);
        if (mer != null) {
            MerchantVo merchant = new MerchantVo();
            merchant.setMid(mer.getMId());
            //这里token直接用一个uuid
            merchant.setToken(UUID.randomUUID().toString());
            merchant.setMerchant(mer);
            return new Result(200, "登录成功", merchant);
        }
        return new Result(400, "登录失败", "");
    }

    //根据商户名查询商户
    public Merchant SelectMerchant(String name) {
        Merchant merchant = merchantMapper.SelectMerchantByName(name);
        return merchant;
    }

    // 创建新商户
    public int RegisterMerchant(int mid, String name, String phone, String password, String email) {
        int resultCount = merchantMapper.RegisterMerchant(mid, name, phone, password, email);
        return resultCount;
    }

    /**
     * 查询商家id的最大值
     * @return
     */
    public int SelectMaxMid(){
        return merchantMapper.SelectMaxMid();
    }

    /**
     * 查询商家店铺名
     * @param mid
     * @return
     */
    public Result findMerchantName(int mid) {
        String name = merchantMapper.findMerchantName(mid);
        return new Result(200,"",name);
    }
}
